package com.dci.service;

import com.dci.context.Config;
import com.dci.dao.pms.XtgOgzInfoMapper;
import com.dci.model.XtgOgzInfo;
import com.dci.param.XtgOgzParam;
import com.dci.result.ExceptionResultInfo;
import com.dci.result.ResultUtil;
import com.dci.util.BeanValidator;
import com.dci.util.LevelUtil;
import com.dci.util.UUIDBuild;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service
 * @Description: 组织机构管理业务层
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class XtgOgzService {

    @Resource
    private XtgOgzInfoMapper xtgOgzInfoMapper;

    /**
     * 新增组织机构
     *
     * @param xtgOgzParam
     * @throws Exception
     */
    public void save(XtgOgzParam xtgOgzParam) throws Exception {
        // 捕获参数异常
        BeanValidator.check(xtgOgzParam);
        if (checkExist(xtgOgzParam.getParentId(), xtgOgzParam.getName(), xtgOgzParam.getId())) {
            ResultUtil.throwExcepion(ResultUtil.createFail(Config.MESSAGE, 201, null));
        }
        XtgOgzInfo xtgOgzInfo = XtgOgzInfo.builder().name(xtgOgzParam.getName()).parentId(xtgOgzParam.getParentId())
                .seq(xtgOgzParam.getSeq()).remark(xtgOgzParam.getRemark()).build();
        xtgOgzInfo.setId(UUIDBuild.getUUID());
        xtgOgzInfo.setLevel("1.1.1");
        xtgOgzInfo.setOperateTime(new Date());
        xtgOgzInfo.setOperator("admin");
        xtgOgzInfo.setOperateId("127.0.0.1");
        xtgOgzInfoMapper.insertSelective(xtgOgzInfo);
    }

    /**
     * 判断同一级目录下组织机构是否重名
     *
     * @param parentId
     * @param name
     * @param ogzId
     * @return
     */
    private boolean checkExist(String parentId, String name, String ogzId) {
        return xtgOgzInfoMapper.countByNameAndParentId(parentId, name, ogzId) > 0;
    }

    /**
     * 获取当前level的值
     *
     * @param ogzId
     * @return
     */
    private String getLevel(String ogzId) {
        XtgOgzInfo xtgOgzInfo = xtgOgzInfoMapper.selectByPrimaryKey(ogzId);
        if (xtgOgzInfo == null) {
            return null;
        }
        return xtgOgzInfo.getLevel();
    }

    /**
     * 更新组织机构
     *
     * @param xtgOgzParam
     * @throws Exception
     */
    public void update(XtgOgzParam xtgOgzParam) throws Exception {
        // 捕获参数异常
        BeanValidator.check(xtgOgzParam);
        XtgOgzInfo beforeOgz = xtgOgzInfoMapper.selectByPrimaryKey(xtgOgzParam.getId());
        // Preconditions.checkNotNull(beforeOgz,"");
        if (beforeOgz == null) {
            ResultUtil.throwExcepion(ResultUtil.createFail(Config.MESSAGE, 902, null));
        }
        if (checkExist(xtgOgzParam.getParentId(), xtgOgzParam.getName(), xtgOgzParam.getId())) {
            throw new ExceptionResultInfo(ResultUtil.createFail(Config.MESSAGE, 201, null));
        }
        XtgOgzInfo after = XtgOgzInfo.builder().id(xtgOgzParam.getId()).name(xtgOgzParam.getName()).parentId(xtgOgzParam.getParentId())
                .seq(xtgOgzParam.getSeq()).remark(xtgOgzParam.getRemark()).build();
        after.setLevel(LevelUtil.calculateLevel(getLevel(xtgOgzParam.getParentId()), xtgOgzParam.getParentId()));
        after.setOperateTime(new Date());
        after.setOperator("admin");
        after.setOperateId("127.0.0.1");
        updateWithChild(beforeOgz, after);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateWithChild(XtgOgzInfo befor, XtgOgzInfo after) {
        xtgOgzInfoMapper.updateByPrimaryKey(after);
        String newLevelPrefix = after.getLevel();
        String oldLevelPrefix = befor.getLevel();
        if (!newLevelPrefix.equals(oldLevelPrefix)) {
            List<XtgOgzInfo> OgzList = xtgOgzInfoMapper.getChildOgzListByLevel(befor.getLevel());
            if (!(CollectionUtils.isEmpty(OgzList))) {
                OgzList.forEach(ogz -> {
                    String level = ogz.getLevel();
                    if (level.indexOf(oldLevelPrefix) == 0) {
                        level = newLevelPrefix + level.substring(oldLevelPrefix.length());
                        ogz.setLevel(level);
                    }
                });
                xtgOgzInfoMapper.batchUpdateLevel(OgzList);
            }
            xtgOgzInfoMapper.updateByPrimaryKey(after);
        }

    }

}
